<template>
  <div>
    <!-- 
          data:数据
          row-key：一行的唯一标识
          border：加边框
          default-expand-all：默认展开所有数据
           :tree-props="{ children: '由哪个字段来判断是否有子节点' }"
       -->
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="商品编号"> </el-table-column>
      <el-table-column prop="goodsname" label="商品名称"> </el-table-column>
      <el-table-column prop="price" label="商品价格"> </el-table-column>
      <el-table-column prop="market_price" label="市场价格"> </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="$pre + scope.row.img" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.isnew == 1">是</el-button>
          <el-button type="danger" v-else>否</el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.ishot == 1">是</el-button>
          <el-button type="danger" v-else>否</el-button>
        </template>
      </el-table-column>

      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.status == 1"
            >启用</el-button
          >
          <el-button type="info" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope)">编辑</el-button>
          <del-btn @confirm="del(scope)"></del-btn>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      layout="prev, pager, next"
      background
      :total="total"
      :page-size="size"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>
<script>
import { reqgoodsdelete} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  computed: {
    ...mapGetters({
      list: "goods/list",
      //取出总数
      total: "goods/total",
      // 取出一页的数量
      size: "goods/size",
    }),
  },
  methods: {
    ...mapActions({
      reqList: "goods/reqList",
      changePage: "goods/changePage",
    }),
    //2.3 点击了编辑按钮
    // 7.1点击了编辑按钮
    handleEdit(scope) {
      //   scope.$index 下标
      // scope.row 那条数据 item
      // console.log(scope);
      //通知父组件点了编辑
      this.$emit("willUpdate", scope.row.id);
    },
    //  删除
    async del(scope) {
      //开始删除
      let res = await reqgoodsdelete({
        id: scope.row.id,
      });
      if (res.data.code == 200) {
        //弹成功
        successAlert(res.data.msg);
        //重新获取list
        this.reqList();
      }
    },
  },
  mounted() {
    //触发
    this.reqList();
  },

  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
img {
  width: 50px;
  height: 50px;
}
</style>